package com.study.lu;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class FindDemo {
    public static void main(String[] args) {
        // int[] A = new int[]{4,5,6,7,5,6,8};
        int[] A = new int[]{4, 5, 6, 7, 5, 6, 8};
        int[] B = new int[]{4, 5, 6};
      //  System.out.println(sub(A, B));

        System.out.println(add("123", "45"));
    }

    public static String add(String numberA, String numberB) {
        if (!verify(numberA) || !verify(numberB)) {
            return "ERROR";
        }
        String[] numArr1 = numberA.split("");
        String[] numArr2 = numberB.split("");
        String[] maxArr, minArr;
        if (numArr1.length >= numArr2.length) {
            maxArr = numArr1;
            minArr = numArr2;
        } else {
            maxArr = numArr2;
            minArr = numArr1;
        }

        StringBuilder sb = new StringBuilder();
        int maxArrIndex = maxArr.length - 1;
        int minArrIndex = minArr.length - 1;
        int shiweishu = 0;
        while (maxArrIndex >= 0) {
            int minGeweishu = minArrIndex<0? 0: Integer.parseInt(minArr[minArrIndex]);
            int sum = Integer.parseInt(maxArr[maxArrIndex]) +minGeweishu + shiweishu;
            if (sum >= 10) {
                int gewei = sum - 10;
                sb.append(gewei);
                shiweishu = 1;
            } else {
                sb.append(sum);
                shiweishu = 0;
            }
            maxArrIndex--;
            minArrIndex--;
        }

        if(shiweishu==1){
            sb.append(1);
        }

        return sb.reverse().toString();
    }

    private static boolean verify(String number) {
        String pattern = "^\\d+";
        Pattern r = Pattern.compile(pattern);
        return r.matcher(number).find();
    }

    public static int sub(int[] A, int[] B) {
        if (null == A || 0 == A.length || null == B || 0 == B.length) {
            return -1;
        }

        if ((null == A || 0 == A.length) && (null == B || 0 == B.length)) {
            return 0;
        }

        if(B.length>A.length){
            return -1;
        }

        String father = Arrays.stream(A).mapToObj(v -> String.valueOf(v)).collect(Collectors.joining());
        String son = Arrays.stream(B).mapToObj(v -> String.valueOf(v)).collect(Collectors.joining());
        int hashSon = son.hashCode();
        int sonLength = son.length();
        String[] sonArr = son.split("");

        int i = father.length() - 1;
        while (i > 0) {
            String substring = father.substring(i - sonLength + 1, i + 1);
            if (substring.hashCode() == hashSon) {
                String[] split = substring.split("");
                boolean isSame = true;
                for (int i1 = 0; i1 < split.length; i1++) {
                    if (!split[i1].equals(sonArr[i1])) {
                        isSame = false;
                    }
                }
                if (isSame) {
                    return i - sonLength + 1;
                }
            }
            i--;
        }


        return -1;
    }
}
